package com.huluobo.a2301mdmdemo.pages

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.ToastUtils
import com.huluobo.lib_base.BaseTitle
import com.huluobo.lib_base.RoutePath
import com.hyphenate.EMValueCallBack
import com.hyphenate.chat.EMClient
import com.hyphenate.chat.EMCursorResult
import com.hyphenate.chat.EMGroup
import com.hyphenate.chat.EMGroupInfo
import com.hyphenate.chat.EMGroupManager
import com.hyphenate.chat.EMGroupOptions
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

/**
 *  Created by LC on 2025/7/9.
 */

@Composable
fun CreateGroupPage(navHostController: NavHostController) {
    //讨论组名字
    var groupName by remember {
        mutableStateOf("")
    }

    //讨论组的描述
    var groupDesc by remember {
        mutableStateOf("")
    }


    Column(
        Modifier
            .fillMaxWidth()
            .padding(20.dp)
    ) {
        BaseTitle(title = "创建讨论组", navHostController = navHostController)

        OutlinedTextField(value = groupName, onValueChange = { groupName = it }, Modifier.fillMaxWidth(), label = {
            Text(text = "讨论组名字")
        })

        OutlinedTextField(value = groupDesc, onValueChange = { groupDesc = it }, Modifier.fillMaxWidth(), label = {
            Text(text = "讨论组描述")
        }, maxLines = 3)

        Button(onClick = {
            //群的配置文件
            val option = EMGroupOptions()
            option.maxUsers = 100
            option.style = EMGroupManager.EMGroupStyle.EMGroupStylePublicOpenJoin
            EMClient.getInstance().groupManager().asyncCreateGroup(
                groupName,
                groupDesc,
                arrayOf(EMClient.getInstance().currentUser),
                "",
                option,
                object : EMValueCallBack<EMGroup> {
                    override fun onSuccess(value: EMGroup?) {
                        runBlocking(Dispatchers.Main) {
                            ToastUtils.showLong("创建讨论组成功")
                            navHostController.popBackStack()
                        }
                    }

                    override fun onError(error: Int, errorMsg: String?) {
                        ToastUtils.showLong("创建讨论组失败:$error,msg:$errorMsg")
                    }
                }
            )
        }) {
            Text(text = "创建讨论组")
        }
    }

}
